package com.example.document

import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.document.databinding.ActivityDocumentBinding
import com.example.quickadapter.BaseQuickAdapter
import com.witmed.lib_base.BaseActivity
import kotlinx.coroutines.launch

@Route(path = "/doc/DocumentActivity")
class DocumentActivity : BaseActivity<ActivityDocumentBinding,DocumentViewModel>() {
    val adapter by lazy { DocAdapter(R.layout.item_doc) }
    override fun createVM(): DocumentViewModel {
        return DocumentViewModel()
    }

    override fun createV(): ActivityDocumentBinding {
        return ActivityDocumentBinding.inflate(layoutInflater)
    }

    override fun bindState() {
        lifecycleScope.launch {
            vm.mutableContainer.uiFlow.collect{
                if(it.docStatue){
                    //把数据添加进适配器数据源
                    adapter.addAll(it.data)
                }
            }
        }
    }

    override fun initData() {
        vm.selDocuments()
    }

    override fun initView() {
        v.rv.layoutManager=LinearLayoutManager(this)
        v.rv.adapter=adapter

        adapter.setOnItemchildClickListener(object:BaseQuickAdapter.OnItemChildClickListener{
            override fun onItemChildClick(id: Int, position: Int) {
                if(id==R.id.tv_delete){
                    adapter.remove(position)
                }
            }

        })

        v.btnUp.setOnClickListener {
            Toast.makeText(this, "上传", Toast.LENGTH_SHORT).show()
        }
    }
}